import django_filters
from django.db.models import Q
from apps.master.models import SerialNumberModel, CurrencyRateModel, DepartmentsModel, CurrencyModel


class SerialNumberFilters(django_filters.rest_framework.FilterSet):
    serial_number = django_filters.CharFilter(field_name='serial_number', lookup_expr='contains')
    serial_name = django_filters.CharFilter(field_name='serial_name', lookup_expr='contains')
    search = django_filters.CharFilter(method='combine_search_filter')

    def combine_search_filter(self, queryset, name, value):
        return queryset.filter(
            Q(serial_number__contains=value) | Q(serial_name__contains=value)
        )

    class Meta:
        model = SerialNumberModel
        fields = '__all__'

class CurrencyFilters(django_filters.rest_framework.FilterSet):
    currency = django_filters.CharFilter(field_name='currency', lookup_expr='contains')

    class Meta:
        model = CurrencyModel
        fields = '__all__'

class CurrencyRateFilters(django_filters.rest_framework.FilterSet):
    standard_curr = django_filters.CharFilter(field_name='standard_curr', lookup_expr='contains')
    quote_curr = django_filters.CharFilter(field_name='quote_curr', lookup_expr='contains')
    rate_date = django_filters.DateFilter(field_name='rate_date', lookup_expr='gte')

    class Meta:
        model = CurrencyRateModel
        fields = '__all__'

class DepartmentsFilters(django_filters.rest_framework.FilterSet):
    code = django_filters.CharFilter(field_name='code', lookup_expr='contains')
    name = django_filters.CharFilter(field_name='name', lookup_expr='contains')
    search = django_filters.CharFilter(method='department_search_filter')

    def department_search_filter(self, queryset, name, value):
        return queryset.filter(
            Q(code__exact=value) | Q(name__contains=value)
        )

    class Meta:
        model = DepartmentsModel
        fields = '__all__'

